<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Addresses;
use App\Models\Orders;
use App\Models\OrderGoods;
use Illuminate\Http\Request;

class OrderController extends Controller {

    /**
     * 页面
     * @return view
     */
    public function index(){
        return view('admin.orders.index');
    }

    /**
     * 列表数据
     * @param Request $request
     * @return bool
     */
    public function list(Request $request){
        try{
            $params = [
                'kwd' => (string)$request->get('kwd',''),
                'order_state' => (int)$request->get('order_state',0),
                'evaluation_state' => (int)$request->get('evaluation_state',0),
                'start' => (string)$request->get('start',''),
                'end' => (string)$request->get('end',''),
            ];
            $lists = Orders::getList($params);

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 详情
     * @param Request $request
     * @return bool|view
     */
    public function detail(Request $request){
        try{
            $id = (int)$request->get('id',0);
            $info = [];
            if($id){
                $info = Orders::findOrFail($id);
                //订单商品
                if(!empty($info)){
                    $info['goods'] = OrderGoods::where(array('order_id'=>$info['id']))->get();
                    foreach ($info['goods'] as $k=>$v){
                        $info['goods'][$k]['goods_type'] = $v['goods_type'] == 1 ? '非实物' : '非实物';
                        $info['goods'][$k]['recommend_goods'] = $v['recommend_goods'] == 1 ? '否' : '是';
                    }
                }else{
                    $goods = array();
                }
                //收货地址信息
                if($info['address_id'] > 0){
                    $info['address'] = Addresses::findOrFail($info['address_id']);
                }else{
                    $info['address'] = array();
                }
                //状态处理
                $info['pay_status_text'] = $info['pay_status'] == 1 ? '未支付' : '已支付';
                $info['shipping_status_text'] = $info['shipping_status'] == 1 ? '未发货' : '已发货';
                $info['order_status_text'] = $info['order_status'] == 1 ? '未收货' : '已收货';
                if($info['order_state'] == 1){
                    $info['order_state_text'] = '已取消';
                }elseif ($info['order_state'] == 2){
                    $info['order_state_text'] = '未付款';
                }elseif ($info['order_state'] == 3){
                    $info['order_state_text'] = '已付款';
                }elseif ($info['order_state'] == 4){
                    $info['order_state_text'] = '已发货';
                }elseif ($info['order_state'] == 5){
                    $info['order_state_text'] = '已收货';
                }
                if($info['pay_method'] == 1){
                    $info['pay_method_text'] = '微信';
                }elseif ($info['pay_method'] == 2){
                    $info['pay_method_text'] = '支付宝';
                }elseif ($info['pay_method'] == 3){
                    $info['pay_method_text'] = '积分兑换';
                }
            }
            $info = json_encode($info);

            return view('admin.orders.orders_detail',compact('info','id'));

        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 发货
     * @param Request $request
     * @return bool
     */
    public function delivery(Request $request){

        try{
            $id = (int)$request->get('id',0);
            $shipping_sn = (string)$request->get('shipping_sn','');
            $shipping_code= (string)$request->get('shipping_code','');

            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            if(empty($shipping_sn)){
                return $this->writeJson(1,null,'发货单号不能为空！');
            }
            if(empty($shipping_code)){
                return $this->writeJson(1,null,'配送方式不能为空！');
            }
            $info = Orders::findOrFail($id);
            $shipping_time = date("Y-m-d H:i:s");
            $update_arr = [];
            $update_arr['shipping_status'] = Orders::SHIPPING_STATUS_2;
            $update_arr['shipping_time'] = $shipping_time;
            $update_arr['shipping_sn'] = $shipping_sn;
            $update_arr['shipping_code'] = $shipping_code;
            $update_arr['order_state'] = Orders::ORDER_STATE_4;
            $info->update($update_arr);
            return $this->writeJson(0,$shipping_time);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 单个/多个删除
     * @param Request $request
     * @return bool
     */
    public function destroy(Request $request){
        try{
            $id = $request->get('id');
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            if(!is_array($id)){
                $id=[$id];
            }
            //修改软删除 19-7-31
            $data['status'] = -1;
            Orders::whereIn('id',$id)->update($data);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

}
